package cn.lingyangwl.agile.system.controller;

import cn.lingyangwl.agile.model.TreeSelectResp;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.model.rqrs.area.*;
import cn.lingyangwl.agile.system.service.SysAreaService;
import cn.lingyangwl.framework.core.response.Resp;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 行政区划
 * 
 * @author shenguangyang
 */
@Api(tags = "行政区划")
@RestController
@RequestMapping("/area")
public class SysAreaController {
    @Resource
    private SysAreaService areaService;

    @ApiOperation(value = "分页行政区划")
    @GetMapping("/page")
    @RamResource(action = "sys:area:query", name = "查询行政区划", type = ActionType.READ)
    public Resp<IPage<AreaResp>> pageArea(@Validated AreaPageReq req) {
        return Resp.ok(areaService.pageArea(req));
    }

    @ApiOperation(value = "查询行政区划")
    @GetMapping("/list")
    @RamResource(action = "sys:area:query", name = "查询行政区划", type = ActionType.READ)
    public Resp<List<AreaResp>> listArea(@Validated AreaListReq req) {
        return Resp.ok(areaService.listArea(req));
    }

    @ApiOperation(value = "通过id查找行政区划")
    @GetMapping("/get")
    @RamResource(action = "sys:area:query", name = "查询行政区划", type = ActionType.READ)
    public Resp<AreaResp> getArea(@RequestParam(value = "areaId") String areaId) {
        return Resp.ok(areaService.getArea(areaId));
    }

    @ApiOperation(value = "通过id查找行政区划")
    @GetMapping("/listTreeSelect")
    @RamResource(action = "sys:area:query", name = "查询行政区划", type = ActionType.READ)
    public Resp<List<TreeSelectResp>> listTreeSelect(AreaListReq req) {
        return Resp.ok(areaService.listTreeSelect(req));
    }
  
    @ApiOperation(value = "保存行政区划")
    @PostMapping("/save")
    @RamResource(action = "sys:area:save", name = "保存行政区划", type = ActionType.WRITE)
    public Resp<?> saveArea(@Validated @RequestBody AreaSaveReq req) {
        return Resp.ok(areaService.saveArea(req));
    }

    @ApiOperation(value = "更新行政区划状态")
    @PutMapping("/updateStatus")
    @RamResource(action = "sys:area:updateStatus", name = "更新行政区划状态", type = ActionType.WRITE)
    public Resp<?> updateStatus(@RequestParam("areaId") String id, @RequestParam("status") String status) {
        areaService.updateStatus(id, status);
        return Resp.ok();
    }
  
    @ApiOperation(value = "更新行政区划")
    @PostMapping("/update")
    @RamResource(action = "sys:area:update", name = "更新行政区划", type = ActionType.WRITE)
    public Resp<?> updateArea(@Validated @RequestBody AreaUpdateReq req) {
        return Resp.ok(areaService.updateArea(req));
    }

    @ApiOperation(value = "删除行政区划")
    @DeleteMapping("/delete")
    @RamResource(action = "sys:area:delete", name = "删除行政区划", type = ActionType.WRITE)
    public Resp<?> deleteArea(@RequestParam(value = "areaId") String areaId) {
        return Resp.ok(areaService.deleteArea(areaId));
    }

    @ApiOperation(value = "通过区域名称查询")
    @GetMapping("/listAreaByNames")
    public Resp<List<SysArea>> listAreaByNames(@RequestParam("areaTypes") List<String> areaTypes,
                                        @RequestParam("areaNames") List<String> areaNames) {
        return Resp.ok(areaService.listAreaByNames(areaTypes, areaNames));
    }
}
